package com.example.homework.adapters

import android.net.Uri
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView.Adapter
import com.bumptech.glide.Glide
import com.example.homework.databinding.PhotoItemBinding
import com.example.homework.db.tables.Photos

class PhotosRecyclerViewAdapter :
    ListAdapter<Photos, PhotosRecyclerViewHolder>(DiffUtilCallback()) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotosRecyclerViewHolder {
        return PhotosRecyclerViewHolder(
            PhotoItemBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    override fun onBindViewHolder(holder: PhotosRecyclerViewHolder, position: Int) {
        val item: Photos? = getItem(position)
        item?.let {
            with(holder.binding) {
                dateTextView.text = item.date
                Glide.with(photoImageView).load(Uri.parse(item.path))
                    .placeholder(android.R.drawable.progress_indeterminate_horizontal)
                    .error(android.R.drawable.stat_notify_error).into(photoImageView)
            }
        }
    }
}